using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using Random = UnityEngine.Random;

public class Monster : MonoBehaviour
{
    private Animator animator;
    [Header("怪物细节信息")] public MonsterDetailSO monsterDetailSO;
    private float attackTimeCount = 0f;
    private int curHealth;
    private Player player;
    private Direction direction;
    private int woundValue;
    private void Awake()
    {
        animator = GetComponent<Animator>();
    }

    // Start is called before the first frame update
    void Start()
    {
        Initialize();
    }

    private void Initialize()
    {
        RuntimeAnimatorController runtimeAnimatorController =
            Resources.Load<RuntimeAnimatorController>(monsterDetailSO.animationControllerPath);
        animator.runtimeAnimatorController = runtimeAnimatorController;

        curHealth = monsterDetailSO.health;
    }

    // Update is called once per frame
    void Update()
    {
        attackTimeCount += Time.deltaTime;
    }


    public void Wound(int value)
    {
        this.curHealth -= value;
        print("怪物损失" + value);
        if (curHealth <= 0)
        {
            curHealth = 0;
            Destroy(gameObject);
        }
    }

    public void Attack(Player player,Direction direction)
    {
        if (attackTimeCount < monsterDetailSO.attackInterval) return;
        this.player = player;
        this.direction = direction;
        animator.SetTrigger("Attack" + direction);
        woundValue = Random.Range(monsterDetailSO.minAtk, monsterDetailSO.maxAtk + 1) -
                         Random.Range(player.minAtk, player.maxAtk + 1);
        if (woundValue <= 0)
        {
            woundValue = 1;
        }
        
        //player.Wound(woundValue);
    }

    public bool IsAlive()
    {
        return curHealth > 0;
    }
    
    public void PlayerHurt()
    {
        if (player!=null)
        {
            player.Wound(woundValue);
            player = null;
        }
    }
}